#!/usr/bin/env ruby
=begin
This script convert a xml generated with "mysql --xml" into another format 
of xml, which can be used on Atom notbook impoting for Google Notebook.

Usage:
To get an input xml file, you can run command like this:
 $ mysql foo --xml -e "select body as content from bar where user_id = 1" > foo.xml
(Be sure, selected column must be named as "content")

Then give it this script:
 $ ruby mysql_xml2google_notebook_atom.rb < foo.xml > foo.atom
After that, you can upload the resulted atom file via Google Notebook's import interface.

KnownIssues:
* Only content of entry, not updated or any other element

ChangeLogs:
0.1 (2008-11-24)
* First release

Authoer: Hiroshi Saito <hiroshi3110@gmail.com>
=end
require "rexml/document"

# input document
input = REXML::Document.new ARGF.read

# output document
output = REXML::Document.new
output << REXML::XMLDecl.new("1.0", "utf-8")
feed = output.add_element("feed", "xmlns" => "http://www.w3.org/2005/Atom")
feed.add_element("title", "type" => "text").text = "tagnote"
# void selection
section_entry = feed.add_element("entry")
section_id = section_entry.add_element("id")
section_id.text = "2"
section_entry.add_element("category", "scheme" => "http://schemas.google.com/g/2005#kind", "term" => 'http://schemas.google.com/notebook/2008/kind#section')
section_entry.add_element("title", "type" => "text")
section_entry.add_element("content", "type" => "text")
# note entries
input.elements.each("resultset/row") do |row|
  entry = feed.add_element("entry")
  entry.add_element("category", "scheme" => 'http://schemas.google.com/notebook/gdata/2007/section', "term" => section_id.text,  "label" => 'title')
  entry.add_element("category", "scheme" => 'http://schemas.google.com/g/2005#kind', "term" => 'http://schemas.google.com/notebook/2008/kind#note')
  entry.add_element("title", "type" => "text")
  entry.add_element("content", "type" => "text").text = row.elements["field[@name='content']"].text
end
# get result
puts output
